//
//  NetworkTooler.m
//  work
//
//  Created by 方伟 on 16/6/1.
//  Copyright © 2016年 方伟. All rights reserved.
//

#import "NetworkTooler.h"
#import "SSKeychain.h"
#import "SSKeychainQuery.h"
#import "CJMD5.h"


@interface NetworkTooler()

@property(nonatomic ,strong ,readonly)NSDictionary *infoDictionary;
@property(nonatomic ,copy)NSString *app_Version;
@property(nonatomic ,copy)NSString *device_info;
@property(nonatomic ,copy)NSString *access_time;
@property(nonatomic ,copy)NSString *device_id;
@property(nonatomic ,copy)NSString *access_token;




@end

@implementation NetworkTooler

-(void)parameters:(NSDictionary *)parameters withURL:(NSString *)URL withIsPaparameters:(BOOL)isPaparameters{
    
    if ([self isConectNet]) {
        
        if (isPaparameters) {
            
            [self configure];
            
            NSDictionary *dic = @{ @"platfrom":platfrom,
                                   @"version":_app_Version,
                                   @"device_info":_device_info,
                                   @"app":worker,
                                   @"access_time":_access_time,
                                   @"access_token":_access_token,
                                   @"device_id":_device_id
                                   
                                   
                                   };
            
            
            parameters = [NSDictionary dictionaryByMerging:dic with:parameters];
            
        }
        
        
        
        
        
            __weak NetworkTooler *weakSelf = self;
        
        [FWNetTool postWithUrl:URL parameters:parameters success:^(id json) {
//            [GiabHUD hideUIBlockingIndicator];
            
            NSString *code = json [@"code"];
            NSString *msg  = json [@"msg"];
            
            
            
            if (code.intValue == 0 ) {
                
                id data;
                
                //过滤
                if (json [@"data"]) {
                    data = json [@"data"];
                }
                
                
                if ([weakSelf.delegate respondsToSelector:@selector(returnData:)]) {
                    [weakSelf.delegate returnData:data];
                }
                
                
            }else {
                
                if ([weakSelf.delegate respondsToSelector:@selector(returnMsg:)]) {
                    [weakSelf.delegate returnMsg:msg];
                }
                
                
                
            }
            
            
        }failure:^(NSError *error) {
        
            
            if ([weakSelf.delegate respondsToSelector:@selector(returnRequestError:)]) {
                [weakSelf.delegate returnRequestError:error];
            }
            
        }];
        
        
        
    }else{
        
        
        if ([self.delegate respondsToSelector:@selector(returnMsg:)]) {
            [self.delegate returnMsg:@"你的网络有问题哦!"];
        }
        
    }
    

    
}


-(void) configure{
    
    
    if (!_device_id) {
        
        _infoDictionary = [[NSBundle mainBundle] infoDictionary];
        // app版本
        _app_Version = [_infoDictionary objectForKey:@"CFBundleShortVersionString"];
        //设备描述
        _device_info = [[UIDevice currentDevice] model];
        //设备唯一标识
        _device_id = [self getDeviceId];
        
    }
    
    //时间戳
    _access_time = [NSString stringWithFormat:@"%.0f" ,[[NSDate date] timeIntervalSince1970]];
    //access_token
    _access_token =  [self md5];
    
    
    
}

- (NSString *)getDeviceId
{
    NSString * currentDeviceUUIDStr = [SSKeychain passwordForService:@" "account:@"uuid"];
    if (currentDeviceUUIDStr == nil || [currentDeviceUUIDStr isEqualToString:@""])
    {
        NSUUID * currentDeviceUUID  = [UIDevice currentDevice].identifierForVendor;
        currentDeviceUUIDStr = currentDeviceUUID.UUIDString;
        currentDeviceUUIDStr = [currentDeviceUUIDStr stringByReplacingOccurrencesOfString:@"-" withString:@""];
        currentDeviceUUIDStr = [currentDeviceUUIDStr lowercaseString];
        [SSKeychain setPassword: currentDeviceUUIDStr forService:@" "account:@"uuid"];
    }
    return currentDeviceUUIDStr;
}

-(NSString *)md5{
    
    
    NSString *str = [_access_time stringByAppendingString:secret];
    //   MD5加密
    NSString *md5 = [CJMD5 md5HexDigest:str];
//    NSLog(@"userName:%@  password: %@",str, md5);
    return md5;
    
}


-(BOOL)isConectNet{
    
    Reachability *reach = [Reachability reachabilityForInternetConnection];
    
    NetworkStatus status = [reach currentReachabilityStatus];
    
    
    switch (status)
    {
        case NotReachable:        {
//            NSLog(@"现在是没有网络环境");
            return NO;
            
            
            break;
        }
            
        case ReachableViaWWAN:        {
//            NSLog(@"现在是移动网络环境");
            return YES;
            
            break;
        }
        case ReachableViaWiFi:        {
//            NSLog(@"现在是WIFI环境");
            return YES;
            
            break;
        }
        default:
            return NO;
            
            break;
            
    }
    return NO;
}

@end
